package com.gui.hospital.listener;

import com.gui.hospital.mapper.HospitalMapper;
import com.gui.hospital.model.Hospital;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

import java.util.List;

/**
 * Author:刘舒佳
 * Date:2024/8/23
 */
@CanalTable("tb_hospital")
@AllArgsConstructor
@Component
public class CanalListenerTab implements EntryHandler<Hospital> {
    private final HospitalMapper hospitalMapper;
    private final RedisTemplate redisTemplate;

    @Override
    public void insert(Hospital hospital) {
        System.out.println("监听到添加数据："+hospital);
        List<Hospital> hospitals = hospitalMapper.selectList(null);
        redisTemplate.boundValueOps("registration_hospitalList").set(hospitals);

    }

    @Override
    public void update(Hospital before, Hospital after) {
        System.out.println("监听到修改后的数据："+after);
        List<Hospital> hospitals = hospitalMapper.selectList(null);
        redisTemplate.boundValueOps("registration_hospitalList").set(hospitals);
    }

    @Override
    public void delete(Hospital hospital) {
        System.out.println("监听到删除数据："+hospital);
        List<Hospital> hospitals = hospitalMapper.selectList(null);
        redisTemplate.boundValueOps("registration_hospitalList").set(hospitals);
    }
}
